package com.data;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.util.Log;

import com.constant.Constant;
import com.net.ClientError;
import com.net.ClientException;
import com.net.RequestParameter;
/**
 * 成绩查询时获取到提交页面中的ViewState
 * 需要的数据
 * <url></url> : 成绩查询接口，登陆时返回
 * <referer></referer> ：成绩查询转发接口，登陆时返回
 * <name></name>姓名
 */
public final class GradeViewStateData extends BaseData
{
	private final static String TAG = "grade_view_sttus_data";
	
	private String mViewState;
	
	@Override
	public void startParse(RequestParameter parameter) throws Exception, ClientException
	{
		Log.i(TAG, "开始获取成绩查询时的viewstate");
		
		String url = parameter.getValue("url");
		
		String referer = parameter.getValue("referer");
		final String name = parameter.getValue("name");
		
		String stream = openConnection(url, referer, name);
		if(stream == null)
			return;
		readStream(stream);
	} // startParse
	
	private String openConnection(final String urls, final String referer, final String name) throws Exception, ClientException
	{
		URL url = new URL(urls.replaceAll(name, URLEncoder.encode(name, "utf-8")));
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setConnectTimeout(Constant.SET_CONNECTION_TIMEOUT);
		connection.setReadTimeout(Constant.SET_READ_TIMEOUT);
		connection.setUseCaches(false);
		connection.setRequestMethod("GET");
		connection.setRequestProperty("Accept", "text/html, application/xhtml+xml, */*");
		connection.setRequestProperty("Referer", referer);
		connection.setRequestProperty("Host", "jwc.xhu.edu.cn");
		connection.connect();
		
		if(connection.getResponseCode() != 200)
		{
			Log.i(TAG, "网络请求错误, 错误代码：" + connection.getResponseCode());
			
			throw new ClientException("网路连接错误", ClientError.ERROR_CONNECTION);
		}
		
//		// 获取网页源代码
//		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "gb2312"));
//		StringBuffer s_html = new StringBuffer();
//		String line = null;
//		while( (line = reader.readLine()) != null)
//		{
//			s_html.append(line + "\n");
//		}
//		reader.close();
//		
////		System.out.println(s_html.toString());
//		return s_html.toString();
		
		return getWebSource(connection.getInputStream(), "gb2312");
		
	} // openConnection
	
	private void readStream(final String stream) throws Exception
	{
		Log.i(TAG, "开始解析Viewstate数据");
		
		Document document = Jsoup.parse(stream);
		Elements elements = document.getElementsByAttributeValue("type", "hidden");
		if ( elements == null )
			throw new Exception("解析html数据失败");
		
		String viewState = elements.get(0).attr("value");
		if ( viewState != null )
		{
			mViewState = viewState;
		}
		
		Log.i(TAG, "解析ViewState数据成功");
		
	} // readStream

	public String getViewState()
	{
		return this.mViewState;
	}
}
